﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KMeansAlgorithm
{
    public class KMeansResult
    {
        public List<ClusteredData> ClusteredDataSet { get; set; }
        public double[][] Centroids { get; set; }

        private double distance(double[] p1, double[] p2)
        {
            if (p1.Length != p2.Length)
            {
                throw new ArgumentException("Lengths of points p1 and p2 should be same! Where p1 is " + p1.Length +
                    " and p2 is " + p2.Length + " long");
            }

            var length = p1.Length;
            var innerResult = 0.0;
            var power = 1.0 / length;

            for (int i = 0; i < length; ++i)
            {
                innerResult += (p2[i] - p1[i]) * (p2[i] - p1[i]);
            }

            var result = Math.Pow(innerResult, power);

            return result;
        }

        public int ClusterOf(double[] data)
        {
            var distances = new List<double>();

            for (int i = 0; i < Centroids.Count(); ++i)
            {
                distances.Add(distance(data, Centroids[i]));
            }

            return distances.IndexOf(distances.Min());
        }
    }
}

